﻿

namespace Boiling.DataAccess
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Text;

    using Boiling.Common;

    public class b_User:IDisposable 
    {
        private SqlDataAdapter dscommand;

        public b_User ()
        {
            
        }

        public void Dispose()
        {
            Dispose(true );
            GC.SuppressFinalize(true );
        }

        protected  virtual void Dispose(bool disposing)
        {
            if(!disposing )
            return ;
            if(dscommand != null )
            {
                if (dscommand .SelectCommand != null)
                {
                    if (dscommand .SelectCommand .Connection != null)
                        dscommand .SelectCommand .Connection .Dispose ();
                    dscommand .SelectCommand .Dispose ();
                }
                dscommand .Dispose ();
                dscommand = null;
            }
        }

        public b_Userdata getuserbyID(string username)
        {
           
            b_Userdata newdata = new b_Userdata();
            string selectstring ="SELECT * FROM b_User WHERE User_ID ="+"'"+username +"'";
            dscommand  = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand .Fill (newdata,"b_User");
            tempconn.Close();
            return (newdata);

        }

        public bool checkrows(b_Userdata newdata)
        {
            if (newdata.Tables["b_User"].Rows.Count == 0)
                return false;
            else
                return true;
        }

        public bool checkuserbyID(string username)
        {
            b_Userdata newdata = getuserbyID (username);
            return (checkrows (newdata ));
        }

        public bool deleteuserbyID(string username)
        {
            b_Userdata newdata = new b_Userdata();
            string selectstring = "SELECT * FROM b_User WHERE User_ID =" + "'" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_User");

            if (newdata.Tables["b_User"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_User"].Rows[0].Delete();
                dscommand.Update(newdata, "b_User");
                tempconn.Close();

                return (true);
            }
            

        }

        public bool modifyuserbydata(b_Userdata newdata)
        {

            string userID = newdata.Tables["b_User"].Rows[0][0].ToString();
            if (checkuserbyID(userID))
            {

                deleteuserbyID(userID);
                insertuserbydata(newdata);
                return true;
            }
            else
                return false;
        }

        public int getNObyID(string username)
        {
            b_Userdata newdata = getuserbyID(username);
            if (newdata.Tables["b_User"].Rows.Count == 0)
                return 0;
            else
            {
                string no = newdata.Tables["b_User"].Rows[0]["User_NO"].ToString();
                int result = Int32.Parse(no);
                return (result);
            }
        }

        public b_Userdata getuserbyNO(int userno)
        {
            //通过用户编号返回一个用户类型
            b_Userdata newdata = new b_Userdata();
            string selectstring = "SELECT * FROM b_User WHERE User_NO =" + userno.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_User");
            tempconn.Close();
            return (newdata);
        }

        public string getIDbyNO(int userno)
        {
            //通过编号返回一个邮箱
            //如果不存在对应编号则返回Fault!
            b_Userdata newdata = getuserbyNO(userno);
            if (newdata.Tables["b_User"].Rows.Count == 0)
                return ("Fault!");
            else
            {
                string result = newdata.Tables["b_User"].Rows[0]["User_ID"].ToString();
                return (result);
            }
        }

    

        public bool insertuserbydata(b_Userdata newdata)
        {
            if (newdata.Tables["b_User"].Rows.Count == 0)
                return false;
            else
            {
                string userid = newdata.Tables["b_User"].Rows[0]["User_ID"].ToString();
                string userpwd = newdata.Tables["b_USer"].Rows[0]["User_Pwd"].ToString();
                SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
                SqlCommand cmd = new SqlCommand("INSERT b_User (User_ID,User_Pwd) VALUES('" + userid + "','" + userpwd + "')", cn);
                cn.Open();
                int records = cmd.ExecuteNonQuery();
                cn.Close();
                if (records == 0)
                    return (false);
                else
                    return (true);
            }
        }

        

        public void IncVisitorcount()
        {
            string selectstring = "SELECT * FROM PageInfo";
            SqlConnection conn = new SqlConnection(BoilingConfiguration.connectionstring );
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring );
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "PageInfo");
            int newcount = Convert.ToInt32(ds.Tables["PageInfo"].Rows[0][0]) + 1;
            ds.Tables["PageInfo"].Rows[0][0] = newcount;
            adapter.Update(ds, "PageInfo");
            conn.Close();
        }
        public int GetVisitorcount()
        {
            string selectstring = "SELECT * FROM PageInfo";
            SqlConnection conn = new SqlConnection(BoilingConfiguration.connectionstring);
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(selectstring, BoilingConfiguration.connectionstring);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "PageInfo");
            int newcount = Convert.ToInt32(ds.Tables["PageInfo"].Rows[0][0]);
            return (newcount);
        }

            





    }
}
